package org.csu.awesome.util;

import java.util.Calendar;
import java.util.Date;

/**
 * @author csucoderlee
 * @version 1.0, Nov 23, 2018
 */
public class DateUtil {

    /**
     * Get the first day of the month specified by date argument.
     * 返回日期参数所在月份的第一天凌晨零点
     * @param date
     * @return
     */
    public static Date getMonthHead(Date date) {
        if (null == date) {
            return null;
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

    /**
     * get zero hour in the morning specified by date argument.
     * 返回日期参数当天的凌晨零点
     * @param date
     * @return
     */
    public static Date getMorning(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

    /**
     * get zero hour in today's morning
     * 获取今天的凌晨零点
     * @return
     */
    public static Date getTodayMorning() {
        return getMorning(new Date());
    }

    public static void main(String[] args) {
        Date date = new Date(System.currentTimeMillis());
        System.out.println(date);
        System.out.println(getMonthHead(date));
        System.out.println(getMorning(date));
        System.out.println(getTodayMorning());

        System.out.println(new Date());
    }
}
